package com.foreveross.fetl.queue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.foreveross.fetl.application.IAirChinaTaobaoApplication;
import com.foreveross.fetl.queue.util.LogMessageUtils;
import com.foreveross.fetl.util.DateUtils;
import com.foreveross.fetl.util.StringUtils;
@Component
public class AirChinaTaobaoPreprocess {
	private static Logger log = Logger.getLogger("airChinaTaobao");
	
	@Inject IAirChinaTaobaoApplication airChinaTaobaoApplication;
	
	private int tasksNumber;//任务数 多少条rowkey
	
	private int failuresNumber;//失败数
	
	private int insertNumber; //插入条数 
	
	private long excuteStartTime;
	
	
	private String taskTime=null;
	
	public void  rollBackNumber(){
		tasksNumber=0;
		failuresNumber=0;
		insertNumber=0;
		excuteStartTime = System.currentTimeMillis();
		LogMessageUtils.create("国航淘宝旗舰代理价差", this.getClass().getSimpleName());
	
	}
	public void execute(){
		final Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR_OF_DAY, -1);
		rollBackNumber();
		taskTime=DateUtils.format(c.getTime(), "yyyy-MM-dd HH");
		try {
			process(c.getTime(), new Date());
		} catch (Exception e) {
			LogMessageUtils.build("国航淘宝旗舰代理价差", this.getClass().getSimpleName(), 3, StringUtils.getStackTrace(e), failuresNumber, tasksNumber, 
					insertNumber, taskTime, excuteStartTime);
		}
		LogMessageUtils.build("国航淘宝旗舰代理价差", this.getClass().getSimpleName(), 2, null, 
				failuresNumber, tasksNumber, insertNumber, taskTime, excuteStartTime);
		
	}
	
	public void manualExecute(final String start,String time){
		this.rollBackNumber();
		try {
		List<String> timeList = Arrays.asList(time.split(";"));
		Calendar c = Calendar.getInstance();
		taskTime = start + " " +timeList.get(0)+ "~" +timeList.get(timeList.size()-1);
		String str =start;
		str += timeList.get(0);
		Date startTime = DateUtils.parse(str, "yyyyMMddHH");
		Date endTime=DateUtils.parse(start+timeList.get(timeList.size()-1), "yyyyMMddHH");
		c.setTime(endTime);
		c.add(Calendar.HOUR_OF_DAY, +1);
		this.process(startTime, c.getTime());
	/*	for (String s : timeList) {
			String str =start;
			str += s;
			Date startTime = DateUtils.parse(str, "yyyyMMddHH");
			c.setTime(startTime);
			c.add(Calendar.HOUR_OF_DAY, +1);
			Date endTime = c.getTime();
			this.process(startTime, endTime);
			}*/
		LogMessageUtils.build("国航淘宝旗舰代理价差", this.getClass().getSimpleName(), 2, null, 
				failuresNumber, tasksNumber, insertNumber, taskTime, excuteStartTime);
		} catch (Exception e) {
			LogMessageUtils.build("国航淘宝旗舰代理价差", this.getClass().getSimpleName(), 3, StringUtils.getStackTrace(e), failuresNumber, tasksNumber, 
					insertNumber, taskTime, excuteStartTime);
		}
	}
	

	private void process(Date start, Date end) {
		log.info("AirChinaDomesticPreprocess start:"
				+ DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
		List<String> listLine =this.getAirLine();
		List<String> channels = this.getCompareChannels();
		List<String> carrierKeys= this.getCarrierKeys();
		for(String str :listLine){
			String[] lines=str.split("-");
			Calendar cFlightDate = Calendar.getInstance();
			cFlightDate.setTime(start);
				try {
					tasksNumber++;
					cFlightDate.add(Calendar.DAY_OF_MONTH, 7);//+7
					insertNumber += airChinaTaobaoApplication.creatReport(channels, carrierKeys, lines[0], lines[1], start, end, cFlightDate.getTime());
					} catch (Exception e) {
					log.error(StringUtils.getStackTrace(e));
					failuresNumber++;
					}
			  try {
					tasksNumber++;
					cFlightDate.add(Calendar.DAY_OF_MONTH, 9);//+7+9=16
					insertNumber += airChinaTaobaoApplication.creatReport(channels, carrierKeys, lines[0], lines[1], start, end, cFlightDate.getTime());
			  	 } catch (Exception e) {
					log.error(StringUtils.getStackTrace(e));
					failuresNumber++;
				 }
			  try {
					tasksNumber++;
					cFlightDate.add(Calendar.DAY_OF_MONTH, 18);//16+18=34
					insertNumber += airChinaTaobaoApplication.creatReport(channels, carrierKeys, lines[0], lines[1], start, end, cFlightDate.getTime());
			  	 } catch (Exception e) {
				  log.error(StringUtils.getStackTrace(e));
				  failuresNumber++;
			  	 }
		}
		
	}
	

	private List<String> getCarrierKeys(){
		String[] carrier_keys={"CA","CZ","MU","SC","3U","HU","ZH","MF"};
		List<String> carrierKeys =new ArrayList<String>();
		for(String str: carrier_keys){
			carrierKeys.add(str);
		}
		return carrierKeys;
	}
	
	/**
	 * 对比的渠道
	 * @return
	 */
	private List<String> getCompareChannels(){
		List<String> chanels=new ArrayList<String>();
		chanels.add("00001"); //淘宝
	//	chanels.add("00002");//去哪儿
		return chanels;
	}
	/**
	 * 航线
	 * @return
	 */
	private List<String> getAirLine(){
		List<String> listLine = new ArrayList<String>();
		String[] lines = {"PEK-CTU","CKG-PEK","CAN-CTU","XIY-PEK","HGH-XMN","HGH-CKG","CAN-CKG","LXA-CTU","XMN-PEK","SHE-PEK"};
		for(String str :lines){
			listLine.add(str);
		}
		return listLine;
	}
	


}
